local hx__daxianzhe = fk.CreateSkill {
  name = "hx__daxianzhe",
  tags = { Skill.Compulsory, },
}

hx__daxianzhe:addEffect(fk.AfterSkillEffect, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__daxianzhe.name) and target and target ~= player and player:usedEffectTimes(self.name) < 20 and
    data.skill.name ~= "hx__daxianzhe" and table.contains(target:getSkillNameList(), data.skill.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local choices = {"hx__daxianzhe_1","hx__daxianzhe_2", "hx__daxianzhe_3", "hx__daxianzhe_4"}
    room:addPlayerMark(player, "@hx__daxianzhe", 1)
    if player:getMark("@hx__daxianzhe") % 5 == 0 then
      local choice = room:askToChoice(player, {
        choices = choices,
        skill_name = hx__daxianzhe.name,
        prompt = "#hx__daxianzhe",
      } )
      if choice == "hx__daxianzhe_1" then
        room:addPlayerMark(player, "@hx__daxianzhe_1", 1)
      elseif choice == "hx__daxianzhe_2" then
        room:addPlayerMark(player, "@hx__daxianzhe_2", 1)
      elseif choice == "hx__daxianzhe_3" then
        room:addPlayerMark(player, "@hx__daxianzhe_3", 1)
      elseif choice == "hx__daxianzhe_4" then
        room:addPlayerMark(player, "@hx__daxianzhe_4", 1)
      end
      room:setPlayerMark(player, "@hx__daxianzhe", 0)
    end
  end,
})

hx__daxianzhe:addEffect(fk.TargetSpecified, {
  anim_type = "offensive",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__daxianzhe.name) and
    data.card and data.card.is_damage_card and data.to ~= player and player:getMark("@hx__daxianzhe_1") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = data.to
    if not to:isAllNude() then
      local cards = room:askToChooseCards(player, {
        target = to,
        min = 0,
        max = player:getMark("@hx__daxianzhe_1"),
        prompt = "#hx__daxianzhe_1-ask",
        flag = "hej",
        skill_name = hx__daxianzhe.name,
      })
      if #cards > 0 then
        room:moveCardTo(cards, Card.DiscardPile, player, fk.ReasonDiscard, hx__daxianzhe.name)
      end
    end
  end,
})

hx__daxianzhe:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__daxianzhe.name) and
      (data.card.name == "jink" or data.card.name == "nullification")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for i = 1 , 1 + player:getMark("@hx__daxianzhe_2") do
      local cards = player.room:getCardsFromPileByRule(".|.|.|.|.|trick")
      if #cards > 0 then
        room:obtainCard(player, cards[1], false, fk.ReasonDraw,player,hx__daxianzhe.name)
      end
    end
  end,
})
hx__daxianzhe:addEffect(fk.CardResponding, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__daxianzhe.name) and
      (data.card.name == "jink" or data.card.name == "nullification")
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for i = 1 , 1 + player:getMark("@hx__daxianzhe_2") do
      local cards = player.room:getCardsFromPileByRule(".|.|.|.|.|trick")
      if #cards > 0 then
        room:obtainCard(player, cards[1], false, fk.ReasonDraw,player,hx__daxianzhe.name)
      end
    end
  end,
})

hx__daxianzhe:addEffect(fk.CardUsing, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(hx__daxianzhe.name) and data.card.type == Card.TypeTrick then
      if target == player then
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    for i = 1 , 1 + player:getMark("@hx__daxianzhe_3") do
      local cards = player.room:getCardsFromPileByRule(".|.|.|.|.|basic")
      if #cards > 0 then
          room:obtainCard(player, cards[1], false, fk.ReasonDraw, player, hx__daxianzhe.name)
      end
    end
  end
})

hx__daxianzhe:addEffect(fk.CardUseFinished, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__daxianzhe.name) then
      if event == fk.CardUseFinished then
        return data.card.type == Card.TypeEquip and target ~= player
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for i = 1 , 1 + player:getMark("@hx__daxianzhe_4") do
      local cards = player.room:getCardsFromPileByRule(".|.|.|.|.|equip")
      if #cards > 0 then
        room:obtainCard(player, cards[1], false, fk.ReasonDraw, player, hx__daxianzhe.name)
      end
    end
  end,
})
hx__daxianzhe:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(hx__daxianzhe.name) then
      if event == fk.AfterCardsMove then
        for _, move in ipairs(data) do
          if move.moveReason == fk.ReasonDiscard then
            for _, info in ipairs(move.moveInfo) do
              if Fk:getCardById(info.cardId).type == Card.TypeEquip then
                return true
              end
            end
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for i = 1 , 1 + player:getMark("@hx__daxianzhe_4") do
      local cards = player.room:getCardsFromPileByRule(".|.|.|.|.|equip")
      if #cards > 0 then
        room:obtainCard(player, cards[1], false, fk.ReasonDraw, player, hx__daxianzhe.name)
      end
    end
  end,
})

return hx__daxianzhe